package com.example.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @User tain
 * @Author zty
 * @Date 2023/7/1 7:54
 * @ProjectName redis-demo
 * @Version
 * redis配置。 如果不配置 在使用lettuce下会使用默认的RedisTemplate
 */
@Configuration
public class RedisConfig {

    //查看是否使用jedispool 可以查看redis.clients.jedis.JedisPoolConfig 以及org.springframework.data.redis.connection.jedis.JedisClientConfiguration

    @Bean
    @Autowired
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory connectionFactory) {
        //这个connectionFactory 会根据redisProperty的 connect-type产生jedis 或 letture的配置。
        //如果引入redisson, 那么用的是RedissonConnectFactory
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }


}
